<?php

/**
 * partecipa actions.
 *
 * @package    cuv
 * @subpackage partecipa
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class partecipaActions extends sfActions
{
/**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->setCampagnaAndAdesioniOrForward($request);

    $this->nAdesioni = CampaignCommentQuery::create()->
            filterByIdCampaign($this->campagna->getId())->
            count();

    $this->commenti = CampaignCommentQuery::create()->
              getCommentListCriteria($this->campagna)->
              paginate($request->getParameter("page"), sfConfig::get('max_per_page', 10));

  }

  public function executeNick(sfWebRequest $request)
  {
    $this->setCampagnaAndAdesioniOrForward($request);

    $this->nAdesioni = CampaignCommentQuery::create()->
            filterByIdCampaign($this->campagna->getId())->
            count();

    $this->commenti = CampaignCommentQuery::create()->
              getNickListCriteria($this->campagna)->
              paginate($request->getParameter("page"), 1000);

  }

  public function executeNew(sfWebRequest $request)
  {
    $this->setCampagnaAndAdesioniOrForward($request);
    
    $this->form = new CampaignCommentForm($this->campagna);
  }

  public function executeOk(sfWebRequest $request)
  {

  }
  
  public function executeCreate(sfWebRequest $request)
  {
    $this->setCampagnaAndAdesioniOrForward($request);
    
    $this->form = new CampaignCommentForm($this->campagna);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }
  
  protected function setCampagnaAndAdesioniOrForward(sfWebRequest $request)
  {
    $idCampagna = $request->getParameter('id');
    
    $this->campagna = CampaignQuery::create()->
            findOneById($idCampagna);
    
    $this->forward404If(!$this->campagna);

    $this->nAdesioni = CampaignCommentQuery::create()->
            filterByIdCampaign($this->campagna->getId())->
            count();
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $captcha = array(
      'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
      'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
    );

    $form->bind($request->getParameter($form->getName()), array('captcha' => $captcha));
    if ($form->isValid())
    {
      $commento = $form->save();

      $this->redirect('@partecipa?action=ok&id='.$this->campagna->getId() . '&slug=' . Cuv::slugify($this->campagna->getName()));
    }
  }
}
